package com.xinhuajs.articlemg.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.xinhuajs.articlemg.common.page.Page;
import com.xinhuajs.articlemg.pojo.Article;
import com.xinhuajs.articlemg.service.IUserArtService;

@Controller
@RequestMapping("/userart")
public class UserArtController {
  
	@Autowired
	private IUserArtService userArtService;
	
	@RequestMapping("/searchartfirst")
	public String searchartfirst(){
		
		return "forward:/WEB-INF/views/umodule/uartmage/uartfirst.jsp";
	}
	
	
	@RequestMapping("/searchartresult")
	public String searchartresult(Model model,@RequestParam(value="pageSize",required=false) Integer pageSize,@RequestParam(value="currPage",required=false) Integer currPage){
		Map<String,Object> paramMap=new HashMap<String,Object>();
		Page page=new Page();
		page.setTotalCount(userArtService.getArticleCount(paramMap));
		if(pageSize!=null&&!"".equals(pageSize)){
			page.setPageSize(pageSize);
		}
		if(currPage!=null&&!"".equals(currPage)&&currPage>=1){
			if(currPage>page.getTotalPage()){
				page.setCurrPage(page.getTotalPage());
			}else{
			page.setCurrPage(currPage);
			}
		}
		page.setParamMap(paramMap);
		System.out.println(page.getCurrPage()+" "+page.getPageSize());
		paramMap.put("startNum",page.getStartNum());
		paramMap.put("size",page.getPageSize());
		userArtService.getArticlesByPage(paramMap);
	    List<Article> articleList= (List<Article>)paramMap.get("rows");
		model.addAttribute("articleList", articleList);
		model.addAttribute("page",page);
		
	    
	    
		return "forward:/WEB-INF/views/umodule/uartmage/scartres.jsp";
	}
	
	
	@RequestMapping("/previewart")
	public String previewart(){
		
		return "forward:/WEB-INF/views/umodule/uartmage/artcover.jsp";
	}
	
	
	@RequestMapping("/getartdetail")
	public String getartdetail(){
		
		return "forward:/WEB-INF/views/umodule/uartmage/artdetail.jsp";
	}
}
